﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace BRS.BidModule
{
    public partial class AddUser : Form
    {
        DateTime datetime;
        Entities brs = new Entities();
        public AddUser()
        {
            InitializeComponent();
        }


        //public Userfrm(Users users)
        //{
        //    InitializeComponent();
        //    //Users user = new Users()
        //    {
        //        textBox1.Text = users.UserName;
        //        //textBox2.Text = users.Password;
        //        textBox3.Text = users.UserCode;
        //        textBox6.Text = users.UkeyCode;
        //        textBox7.Text = users.UkeyPassword;
        //        checkBox1.Checked = Enabled;
        //        datetime = users.AddTime;


        //    };
        //}
        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text != null && textBox1.Text != "")
            {
                if (brs.Users.Where(p => p.UserName == textBox1.Text).Count() > 0)
                { MessageBox.Show("该用户名已经存在，请选择其他用户名！"); }
                else
                {
                    if (datetime == null) { datetime = DateTime.Now; }
                    byte[] result = Encoding.Default.GetBytes(this.textBox2.Text.Trim());    //tbPass为输入密码的文本框
                    MD5 md5 = new MD5CryptoServiceProvider();
                    byte[] output = md5.ComputeHash(result);
                    string MD5password = BitConverter.ToString(output).Replace("-", "");  //tbMd5pass为输出加密文本的文本框
                    Users user = new Users()
                    {
                        UserName = textBox1.Text,
                        Password = MD5password,
                        UserCode = textBox3.Text,
                        UkeyCode = textBox6.Text,
                        UkeyPassword = textBox7.Text,
                        Enabled = checkBox1.Checked,
                        AddTime = DateTime.Now

                    };
                    brs.Users.Add(user);
                    int ok = brs.SaveChanges();
                    if (ok == 1) MessageBox.Show("添加成功！");
                }

            }
        }
    }
}
